﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using emp.BLL.SysManage;
using emp.Model.SysManage;
using emp.BLL;
using emp.Model;
using emp.ProcessFlow;
public partial class crm_individuals_ExternalCal : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (string.IsNullOrEmpty(Request.Params["QunaireID"]) == false)
            {
                //获得此记录所在页面数
                string sCurPage = Request.Params["cur_page"];
                if (string.IsNullOrEmpty(sCurPage))
                {
                    sCurPage = "0";
                }
                hfpage.Value = sCurPage;

                hfquestid.Value = Request.Params["QunaireID"].ToString();

                hfcacuid.Value = Request.Params["cacuid"] != null ? Request.Params["cacuid"].ToString() : "1";
                hfindurn.Value = Request.Params["indurn"] != null ? Request.Params["indurn"].ToString() : "1";
                hfinouid.Value = Request.Params["inouid"] != null ? Request.Params["inouid"].ToString() : "1";
                DataTable dt=new emp.BLL.Individuals().getInoByInouid(hfinouid.Value);
                if(dt.Rows.Count>0)
                {
                    this.hftraceflag.Value = dt.Rows[0][0].ToString();
                }
                 else
                {
                     this.hftraceflag.Value = "0";
                }
                
                    //Request.Params["traceflag"] != null ? Request.Params["traceflag"].ToString() : "0";

            }
        }
        else
        {
            if (Int32.Parse(hftraceflag.Value) == 0)
            {
                string sqlupdate = "";
                string traceflag = Request.Params["rdtrace"] != null ? Request.Params["rdtrace"].ToString() : "0";
                string tracerem = Request.Params["textareatrace"] != null ? Request.Params["textareatrace"].ToString() : "";
                sqlupdate += "UPDATE INDIVIDUAL_DEALERS SET ILD_TRACK_FLAG='" + traceflag + "',ILD_TRACK_DATE=GETDATE(),ILD_TRACK_REMARK='" + tracerem + "' WHERE ILD_INO_UID=" + hfinouid.Value + "";
                //Response.Write(sqlupdate);

                Questionnaire qu = new Questionnaire();
                if (qu.executesql(sqlupdate) > -1)
                {
                    Response.Write("<script>alert('保存成功！');window.location.href='DealersTraceList.aspx?cur_page=" + hfpage.Value + "';</script>");
                }
            }

        }
        initialvalue();
        if (this.hftraceflag.Value == "0")
        {
            this.Panel1.Visible = false;
        }
    }
    void initialvalue()
    {
        Campaign cam = new Campaign();
        CampaignInfo caminfo = cam.getDealerTraceCaminfo(Int32.Parse(hfinouid.Value));
        DateTime obtime = DateTime.Parse(caminfo.Cam_endtime);
        lb_obtime.Text = obtime.Year.ToString() + "年" + obtime.Month.ToString() + "月" + obtime.Day.ToString() + "日  " + obtime.Hour.ToString() + "时" + obtime.Minute.ToString() + "分";
        this.txt_camname_alias.Text = caminfo.cam_show_desc;
        this.lb_camname.Text = caminfo.cam_show_name;

        QunaireQuest myQuest = new QunaireQuest();
        quest_list.Text = "";

        Questionnaire qu = new Questionnaire();
        // quest_list.Text = qu.gethtmlscript(int.Parse(hfquestid.Value));
        // if (quest_list.Text == "")
        // {
        //quest_list.Text = myQuest.getPartQuestPreview(int.Parse(hfquestid.Value),"2");

        quest_list.Text = myQuest.getPartQuestPreview(int.Parse(hfquestid.Value));
        // }
        string[] column = qu.getcolumnview(int.Parse(hfquestid.Value), "A").Split(',');
        IList valuelist = qu.getcolumvalueview(int.Parse(hfquestid.Value), "A", Int32.Parse(hfinouid.Value));
        string[,] columnvalue = (string[,])valuelist[0];

        string str = "<script>";
        //问卷信息
        for (int i = 0; i < column.Length; i++)
        {
            switch (columnvalue[i, 2])
            {
                case "T":
                    str += "document.all." + columnvalue[i, 1] + ".value='" + columnvalue[i, 0] + "';\n";
                    str += "document.all." + columnvalue[i, 1] + ".disabled=true;\n";
                    break;
                case "K":
                    str += "document.all." + columnvalue[i, 1] + ".value='" + columnvalue[i, 0] + "';\n";
                    str += "document.all." + columnvalue[i, 1] + ".disabled=true;\n";
                    break;
                case "D":
                    str += "for(k=0;k<document.all." + columnvalue[i, 1] + ".length;k++){\n"
                   + "if(document.all." + columnvalue[i, 1] + "[k].value=='" + columnvalue[i, 0] + "')\n"
                   + "{document.all." + columnvalue[i, 1] + "[k].selected=true;"
                   + "}"
                   + "}\n";
                    if (columnvalue[i, 3] == "Y")//如果是跳转问题
                    {
                        int activepartid = qu.getactivepartid(columnvalue[i, 0], Int32.Parse(columnvalue[i, 4]));
                        if (activepartid > 0)
                        {
                            str += "document.all.table" + activepartid + ".style.display=\"block\";";
                        }
                    }
                    if (columnvalue[i, 5] == "Y")//如果是关联问题
                    {
                        string[] relateinfo = qu.getrelatequestinfo(Int32.Parse(columnvalue[i, 4])).Split(',');
                        str += "changelocation(document.all." + columnvalue[i, 1] + ".options[document.all." + columnvalue[i, 1] + ".selectedIndex].value,'" + relateinfo[0] + "','" + relateinfo[1] + "');\n";
                    }
                    str += "document.all." + columnvalue[i, 1] + ".disabled=true;\n";
                    break;
                case "C":
                    str += "for(k=0;k<document.all." + columnvalue[i, 1] + ".length;k++){\n"
                        // + "if(document.all." + columnvalue[i, 1] + "[k].value=='" + columnvalue[i, 0] + "')\n"
                  + "if('" + columnvalue[i, 0] + "'.indexOf(document.all." + columnvalue[i, 1] + "[k].value)>-1)\n"
                   + "{document.all." + columnvalue[i, 1] + "[k].checked=true;"
                   + "}document.all." + columnvalue[i, 1] + "[k].disabled=true;"
                   + "}\n";
                    break;
                case "R":
                    str += "for(k=0;k<document.all." + columnvalue[i, 1] + ".length;k++){\n"
                   + "if(document.all." + columnvalue[i, 1] + "[k].value=='" + columnvalue[i, 0] + "')\n"
                   + "{document.all." + columnvalue[i, 1] + "[k].checked=true;"
                   + "}document.all." + columnvalue[i, 1] + "[k].disabled=true;"
                   + "}\n";

                    if (columnvalue[i, 3] == "Y")//如果是跳转问题
                    {
                        int activepartid = qu.getactivepartid(columnvalue[i, 0], Int32.Parse(columnvalue[i, 4]));
                        if (activepartid > 0)
                        {
                            str += "document.all.table" + activepartid + ".style.display=\"block\";";
                        }
                    }
                    break;
            }
            

        }

        //拨打结果
        string[] dailresult = qu.getdailresult(Int32.Parse(hfinouid.Value)).Split('%');
        str += "for(k=0;k<document.all.rddailresult.length;k++){\n"
                   + "if(document.all.rddailresult[k].value=='" + dailresult[0] + "')\n"
                   + "{document.all.rddailresult[k].checked=true;}}\n";
        str += "document.all.textarearemark.value='" + dailresult[1].Replace("\r\n", "\\r\\n") + "';\n";
        str += "document.all.textareadvice.value='" + dailresult[2].Replace("\r\n", "\\r\\n") + "';\n";

        //跟踪结果
        if (Int32.Parse(hftraceflag.Value) > 0)//如果拨打过
        {
            string[] traceresult = qu.gettraceresult(Int32.Parse(hfinouid.Value)).Split('%');
            str += "for(k=0;k<document.all.rdtrace.length;k++){\n"
                       + "if(document.all.rdtrace[k].value=='" + traceresult[0].Replace("\r\n", "\\r\\n") + "')\n"
                       + "{document.all.rdtrace[k].checked=true;}document.all.rdtrace[k].disabled=true;}\n";
            str += "document.all.textareatrace.value='" + traceresult[1].Replace("\r\n", "\\r\\n") + "';\n";
            str += "document.all.textareatrace.disabled=true;\n";
           // str += "document.all.btnSubmit.disabled=true;\n";//btnSubmit
        }

        str += "for(m=0;m<document.all.length;m++){"
           + "if(document.all[m].type=='button'&&document.all[m].value=='...'){"
           + "document.all[m].disabled=true;}}";
        str += "</script>";

        this.lbscript.Text = str;


        LeadCreate lc = new LeadCreate();
        Campaign_ChannelInfo cinfo = new Campaign_ChannelInfo();
        cinfo = lc.getAssginMode(int.Parse(hfcacuid.Value));
        if (cinfo.iCAC_ASSIGN_MODE.Equals("1"))
        {
            IBChargeInfo info = new IBChargeInfo();
            info = lc.getLeadMode(int.Parse(hfinouid.Value));
            if (info.INO_IS_LEADS.Equals("Y"))
            {
                islead.Checked = true;
            }
            if (info.INO_ATTEND_CAMPAIGN.Equals("Y"))
            {
                attend.Checked = true;
            }
        }
        else
        {
            this.assignmode.Style.Add("display", "none");

        }

    }
 
   
}
